import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from typing import Optional


from langchain_core.tools import tool
from mysql.connector import connect
from tools.get_mysql import MySQLConnectionFactory
import random
import datetime
from tools.utility import timer_decorator

mysql_factory = MySQLConnectionFactory()

@tool
@timer_decorator
def check_orders(order_number: str) -> str:

    """根据订单编号查询订单信息。

    Args:
        order_number (str): 订单编号


    Returns:
        dict: 包含以下字段的订单信息:
            - order_id (int): 订单ID 
            - order_number (str): 订单编号
            - order_status (str): 订单状态,如待付款、已付款、已发货等
            - order_time (str): 下单时间
            - customer_id (int): 客户ID
            - customer_name (str): 客户姓名 
            - customer_contact (str): 客户联系方式
            - item_id (str): 商品ID
            - product_name (str): 商品名称
            - product_quantity (int): 商品数量
            - product_price (float): 商品单价
            - total_amount (float): 订单总价
            - discount_amount (float): 优惠金额
            - paid_amount (float): 实际支付金额
            - shipping_address (str): 收货地址
            - shipping_method (str): 配送方式
            - tracking_number (str): 物流单号
            - payment_method (str): 支付方式
            - payment_time (str): 支付时间
            - payment_status (str): 支付状态,如未支付、已支付、支付失败等
    """
    query = f"""
    SELECT * FROM orders WHERE order_number = '{order_number}'
    """
    results = mysql_factory.execute_query(query)
    return results




@tool
@timer_decorator
def update_shipping_address(order_number: str, address: str) -> str:
    """更新订单的收货地址。


    Args:
        order_number (str): 订单编号
        address (str): 新的收货地址


    Returns:
        str: 更新收货地址结果
    """
    query = f"""
    UPDATE orders SET shipping_address = '{address}' WHERE order_number = '{order_number}'
    """


    mysql_factory.execute_update(query)
    return f"收货地址更新成功"


orders_safe_tools = [check_orders]
orders_sensitive_tools = [update_shipping_address]

if __name__ == "__main__":
    # print(check_orders.invoke({"order_number": "2025020811234135952947"}))

    print(update_shipping_address.invoke({"order_number": "2025020811234135952947", "address": "北京市海淀区宝胜南路奥北科技园"}))
   